package com.ruoyi.system.service.manager.impl;

import java.util.List;

import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.manager.DevNacos;
import com.ruoyi.system.domain.manager.NacosConfigInfo;
import com.ruoyi.system.domain.vo.NacosRequest;
import com.ruoyi.system.mapper.manager.DevNacosMapper;
import com.ruoyi.system.mapper.manager.NacosConfigInfoMapper;
import com.ruoyi.system.service.manager.IDevNacosService;
import com.ruoyi.system.service.manager.INacosConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * nacos服务器的配置Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-01
 */
@Service
@Slf4j
public class DevNacosServiceImpl implements IDevNacosService {
    @Resource
    private DevNacosMapper devNacosMapper;
    @Autowired
    private INacosConfigService nacosConfigService;
    @Resource
    private NacosConfigInfoMapper nacosConfigInfoMapper;
    /**
     * 查询nacos服务器的配置
     *
     * @param id nacos服务器的配置主键
     * @return nacos服务器的配置
     */
    @Override
    public DevNacos selectDevNacosById(Long id) {
        return devNacosMapper.selectDevNacosById(id);
    }

    /**
     * 查询nacos服务器的配置列表
     *
     * @param devNacos nacos服务器的配置
     * @return nacos服务器的配置
     */
    @Override
    public List<DevNacos> selectDevNacosList(DevNacos devNacos) {
        return devNacosMapper.selectDevNacosList(devNacos);
    }

    @Override
    public List<NacosConfigInfo> selectNacosConfigInfoList(NacosConfigInfo nacosConfigInfo) {
        return nacosConfigInfoMapper.selectNacosConfigInfoList(nacosConfigInfo);
    }

    /**
     * 新增nacos服务器的配置
     *
     * @param devNacos nacos服务器的配置
     * @return 结果
     */
    @Override
    public int insertDevNacos(DevNacos devNacos) {
        devNacos.setCreateTime(DateUtils.getNowDate());
        return devNacosMapper.insertDevNacos(devNacos);
    }

    /**
     * 修改nacos服务器的配置
     *
     * @param devNacos nacos服务器的配置
     * @return 结果
     */
    @Override
    public int updateDevNacos(DevNacos devNacos) {
        devNacos.setUpdateTime(DateUtils.getNowDate());
        return devNacosMapper.updateDevNacos(devNacos);
    }

    /**
     * 批量删除nacos服务器的配置
     *
     * @param ids 需要删除的nacos服务器的配置主键
     * @return 结果
     */
    @Override
    public int deleteDevNacosByIds(Long[] ids) {
        return devNacosMapper.deleteDevNacosByIds(ids);
    }

    /**
     * 删除nacos服务器的配置信息
     *
     * @param id nacos服务器的配置主键
     * @return 结果
     */
    @Override
    public int deleteDevNacosById(Long id) {
        return devNacosMapper.deleteDevNacosById(id);
    }

    @Override
    public boolean updateNacosConfig(NacosRequest request) {
        //String dataId, String group, String propertyName, String newValue
        try {
            nacosConfigService.updatePropertyInYamlConfig(request.getDataId(), request.getGroup(), request.getPropertyName(), request.getNewValue());
            return true;
        } catch (NacosException e) {
            throw new RuntimeException(e);
        }
    }


}
